<!DOCTYPE html>
<html lang="en">

<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
	<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
	
	<!-- title -->
	
	<title>
	
		ThreadLocal | 
	 
	对线面试官
	</title>
	
	<!-- keywords,description -->
	 
		<meta name="description" content="Java一线大厂面试" />
	

	<!-- favicon -->
	
	<link rel="shortcut icon" href="/luffy/favicon.ico">
	


	<!-- search -->
	<script>
		var searchEngine = "https://www.baidu.com/s?wd=";
		if(typeof searchEngine == "undefined" || searchEngine == null || searchEngine == ""){
			searchEngine = "https://www.google.com/search?q=";
		}
		var homeHost = "";
		if(typeof homeHost == "undefined" || homeHost == null || homeHost == ""){
			homeHost = window.location.host;
		}
	</script>


	
<link rel="stylesheet" href="/luffy/css/main.css">

	
<link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.min.css">

	
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.17.1/build/styles/darcula.min.css">

	
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">


	
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>

	
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

	
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.17.1/build/highlight.min.js"></script>

	
<script src="https://cdn.jsdelivr.net/npm/jquery-pjax@2.0.1/jquery.pjax.min.js"></script>

	
<script src="/luffy/js/main.js"></script>

	
		
<script src="https://cdn.jsdelivr.net/npm/leancloud-storage/dist/av-min.js"></script>

		
<script src="https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js"></script>

	
	
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
	<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?3efe99c287df5a1d6f0d02d187e403c1";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

<header id="header">
    <a id="title" target="_blank" rel="noopener" href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzU4NzA3MTc5Mg==&action=getalbum&album_id=1657204970858872832&scene=126#wechat_redirect" class="logo">对线面试官</a>

	<ul id="menu">
		
	

	

		<li class="menu-item">
			<a href="https://gitee.com/zhongfucheng/Java3y" class="menu-item-link" target="_blank">
				【戳这里】获取更多原创干货💪💪
			</a>
		</li>
		<li class="menu-item">
			<a href="https://gitee.com/zhongfucheng/Java3y" class="menu-item-link" target="_blank">
				<i class="fa fa-github fa-2x"></i>
			</a>
		</li>
	</ul>
</header>

	
<div id="sidebar">
	<button id="sidebar-toggle" class="toggle" ><i class="fa fa-arrow-right " aria-hidden="true"></i></button>
	
	<div id="site-toc">
		<input id="search-input" class="search-input" type="search" placeholder="按回车全站搜索">
		<div id="tree">
			

			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										00-面试前准备
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/10/30/00-%E9%9D%A2%E8%AF%95%E5%89%8D%E5%87%86%E5%A4%87/01.%E7%AE%80%E5%8E%86/">
										01.简历
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										01-Java基础
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/01-Java%E5%9F%BA%E7%A1%80/01.%20%E6%B3%A8%E8%A7%A3/">
										01. 注解
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/01-Java%E5%9F%BA%E7%A1%80/02.%20%E6%B3%9B%E5%9E%8B/">
										02. 泛型
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/01-Java%E5%9F%BA%E7%A1%80/03.%20JavaNIO/">
										03. JavaNIO
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/01-Java%E5%9F%BA%E7%A1%80/04.%20%E5%8F%8D%E5%B0%84%E5%92%8C%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86/">
										04. 反射和动态代理
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										02-Java并发
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/02-Java%E5%B9%B6%E5%8F%91/01.%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%9F%BA%E7%A1%80/">
										01. 多线程基础
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/02-Java%E5%B9%B6%E5%8F%91/02.%20CAS/">
										02. CAS
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/02-Java%E5%B9%B6%E5%8F%91/03.%20synchronized/">
										03. synchronized
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/02-Java%E5%B9%B6%E5%8F%91/04.%20AQS%E5%92%8CReentrantLock/">
										04. AQS和ReentrantLock
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/02-Java%E5%B9%B6%E5%8F%91/05.%20%E7%BA%BF%E7%A8%8B%E6%B1%A0/">
										05. 线程池
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file active">
									<a href="/luffy/2021/08/19/02-Java%E5%B9%B6%E5%8F%91/06.%20ThreadLocal/">
										06. ThreadLocal
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/02-Java%E5%B9%B6%E5%8F%91/07.%20CountDownLatch%E5%92%8CCyclicBarrier/">
										07. CountDownLatch和CyclicBarrier
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/02-Java%E5%B9%B6%E5%8F%91/08.%20%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81Java%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B/">
										08. 为什么需要Java内存模型
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/02-Java%E5%B9%B6%E5%8F%91/09.%20%E6%B7%B1%E5%85%A5%E6%B5%85%E5%87%BAJava%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B/">
										09. 深入浅出Java内存模型
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										03-Java容器
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/03-Java%E5%AE%B9%E5%99%A8/01.%20List%E9%9B%86%E5%90%88/">
										01. List集合
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/03-Java%E5%AE%B9%E5%99%A8/02.%20Map%E9%9B%86%E5%90%88/">
										02. Map集合
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										04-Java虚拟机
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/04-Java%E8%99%9A%E6%8B%9F%E6%9C%BA/01.%20Java%E7%BC%96%E8%AF%91%E5%88%B0%E6%89%A7%E8%A1%8C%E7%9A%84%E8%BF%87%E7%A8%8B/">
										01. Java编译到执行的过程
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/04-Java%E8%99%9A%E6%8B%9F%E6%9C%BA/02.%20%E5%8F%8C%E4%BA%B2%E5%A7%94%E6%B4%BE%E6%9C%BA%E5%88%B6/">
										02. 双亲委派机制
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/04-Java%E8%99%9A%E6%8B%9F%E6%9C%BA/03.%20JVM%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84/">
										03. JVM内存结构
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/04-Java%E8%99%9A%E6%8B%9F%E6%9C%BA/04.%20%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E6%9C%BA%E5%88%B6/">
										04. 垃圾回收机制
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/20/04-Java%E8%99%9A%E6%8B%9F%E6%9C%BA/05.%20CMS%E5%9E%83%E5%9C%BE%E6%94%B6%E9%9B%86%E5%99%A8/">
										05. CMS垃圾收集器
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/20/04-Java%E8%99%9A%E6%8B%9F%E6%9C%BA/06.%20G1%E5%9E%83%E5%9C%BE%E6%94%B6%E9%9B%86%E5%99%A8/">
										06. G1垃圾收集器
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/24/04-Java%E8%99%9A%E6%8B%9F%E6%9C%BA/07.%20JVM%E8%B0%83%E4%BC%98/">
										07. JVM调优
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										05-Spring
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/05-Spring/01.%20SpringMVC/">
										01. SpringMVC
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/05-Spring/02.%20Spring%E5%9F%BA%E7%A1%80/">
										02. Spring基础
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/05-Spring/03.%20SpringBean%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/">
										03. SpringBean生命周期
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										06-Redis
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/06-Redis/01.%20Redis%E5%9F%BA%E7%A1%80/">
										01. Redis基础
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/06-Redis/02.%20Redis%E6%8C%81%E4%B9%85%E5%8C%96/">
										02. Redis持久化
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/20/06-Redis/03.%20Redis%E4%B8%BB%E4%BB%8E%E6%9E%B6%E6%9E%84/">
										03. Redis主从架构
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/20/06-Redis/04.%20Redis%E5%88%86%E7%89%87%E9%9B%86%E7%BE%A4/">
										04. Redis分片集群
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										07-消息队列
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/07-%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97/01.%20Kafka%E5%9F%BA%E7%A1%80/">
										01. Kafka基础
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/07-%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97/02.%20%E4%BD%BF%E7%94%A8Kafka%E4%BC%9A%E8%80%83%E8%99%91%E4%BB%80%E4%B9%88%E9%97%AE%E9%A2%98/">
										02. 使用Kafka会考虑什么问题
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										08-MySQL
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/08-MySQL/01.%20MySQL%E7%B4%A2%E5%BC%95/">
										01. MySQL索引
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/08-MySQL/02.%20MySQL%E4%BA%8B%E5%8A%A1%E5%92%8C%E9%94%81%E6%9C%BA%E5%88%B6%E5%92%8CMVCC/">
										02. MySQL事务和锁机制和MVCC
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/19/08-MySQL/03.%20MySQL%E8%B0%83%E4%BC%98/">
										03. MySQL调优
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										09-项目场景类
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/20/09-%E9%A1%B9%E7%9B%AE%E5%9C%BA%E6%99%AF%E7%B1%BB/01.%20%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E5%8E%BB%E9%87%8D%E5%92%8C%E5%B9%82%E7%AD%89/">
										01. 如何实现去重和幂等
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/20/09-%E9%A1%B9%E7%9B%AE%E5%9C%BA%E6%99%AF%E7%B1%BB/02.%20%E7%B3%BB%E7%BB%9F%E9%9C%80%E6%B1%82%E5%A4%9A%E5%8F%98%E5%A6%82%E4%BD%95%E8%AE%BE%E8%AE%A1/">
										02. 系统需求多变如何设计
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/20/09-%E9%A1%B9%E7%9B%AE%E5%9C%BA%E6%99%AF%E7%B1%BB/03.%20%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">
										03. 设计模式
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										10-计算机网络
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/08/25/10-%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/01.%20HTTP/">
										01. HTTP
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/10/13/10-%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/02.TCP%E4%B8%89%E6%AC%A1%E6%8F%A1%E6%89%8B%E5%92%8C%E5%9B%9B%E6%AC%A1%E6%8C%A5%E6%89%8B/">
										02.TCP三次握手和四次挥手
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										11-算法
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/09/01/11-%E7%AE%97%E6%B3%95/01.%20%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/">
										01. 排序算法
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/09/02/11-%E7%AE%97%E6%B3%95/02.%20LeetCode%20Easy/">
										02. LeetCode Easy
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/09/02/11-%E7%AE%97%E6%B3%95/02.%20LeetCode%20Medium/">
										02. LeetCode Medium
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
							<ul>
								<li class="directory">
									<a href="#" class="directory">
										<i class="fa fa-plus-square-o"></i>
										12-austin项目
									</a>
									
							<ul>
								<li class="file">
									<a href="/luffy/2021/11/01/12-austin%E9%A1%B9%E7%9B%AE/00.%E9%A1%B9%E7%9B%AE%E4%BB%8B%E7%BB%8D/">
										00.项目介绍
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/10/30/12-austin%E9%A1%B9%E7%9B%AE/01.%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/">
										01.环境搭建
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/10/30/12-austin%E9%A1%B9%E7%9B%AE/02.%E6%97%A5%E5%BF%97/">
										02.日志
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/11/24/12-austin%E9%A1%B9%E7%9B%AE/03.%E5%B7%A5%E5%85%B7%E5%8C%85/">
										03.工具包
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/11/24/12-austin%E9%A1%B9%E7%9B%AE/04.%E4%BB%8E%E4%B8%80%E6%9D%A1%E7%9F%AD%E4%BF%A1%E5%BC%80%E5%A7%8B/">
										04.从一条短信开始
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/11/24/12-austin%E9%A1%B9%E7%9B%AE/05.%E9%80%89%E6%8B%A9%E6%95%B0%E6%8D%AE%E5%BA%93%E5%92%8C%E6%8A%80%E6%9C%AF%E9%80%89%E5%9E%8B/">
										05.选择数据库和技术选型
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/11/24/12-austin%E9%A1%B9%E7%9B%AE/06.MySQL%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/">
										06.MySQL环境搭建
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/luffy/2021/11/24/12-austin%E9%A1%B9%E7%9B%AE/07.Kafka%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/">
										07.Kafka环境搭建
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
								</li>
								
							</ul>
			
		</div>
	</div>
</div>

	<!-- 引入正文 -->
	<div id="content">
		<h1 id="article-title">

	06. ThreadLocal
</h1>
<div class="article-meta">
	
	<span>3y</span>
	<span>2021-08-19 13:59:24</span>
		<div id="article-categories">
    
		<span>Categories：</span>
            
    

    
		<span>Tags：</span>
            
    
		</div>

</div>

<div id="article-content">
	<p><span style="color:#ab4642"><strong>面试官</strong></span>：<strong>今天要不来聊聊ThreadLocal吧？</strong></p>
<p><span style="color:#000080"><strong>候选者</strong></span>：我个人对ThreadLocal理解就是</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：它提供了线程的局部变量，每个线程都可以通过set/get来对这个局部变量进行操作</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：不会和其他线程的局部变量进行冲突，实现了线程的数据隔离</p>
<p><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtt5kb6inoj619g0j4tc202.jpg"></p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：<strong>你在工作中有用到过ThreadLocal吗？</strong></p>
<p><span style="color:#000080"><strong>候选者</strong></span>：这块是真不多，不过还是有一处的。就是我们项目有个的DateUtils工具类</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：这个工具类主要是对时间进行格式化</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：格式化/转化的实现是用的SimpleDateFormat</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：但众所周知SimpleDateFormat不是线程安全的，所以我们就用ThreadLocal来让每个线程装载着自己的SimpleDateFormat对象</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：以达到在格式化时间时，线程安全的目的</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：在方法上创建SimpleDateFormat对象也没问题，但每调用一次就创建一次有点不优雅</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：在工作中ThreadLocal的应用场景确实不多，但要不我给你讲讲Spring是怎么用的？</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：好吧，你讲讲呗</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：Spring提供了事务相关的操作，而我们知道事务是得保证一组操作同时成功或失败的</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：这意味着我们一次事务的所有操作需要在同一个数据库连接上</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：但是在我们日常写代码的时候是不需要关注这点的</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：Spring就是用的ThreadLocal来实现，ThreadLocal存储的类型是一个Map</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：Map中的key 是DataSource，value 是Connection（为了应对多数据源的情况，所以是一个Map）</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：用了ThreadLocal保证了同一个线程获取一个Connection对象，从而保证一次事务的所有操作需要在同一个数据库连接上</p>
<p><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtt5soknnhj61ua0f8tdj02.jpg"></p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：了解</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：<strong>你知道ThreadLocal内存泄露这个知识点吗？</strong></p>
<p><span style="color:#000080"><strong>候选者</strong></span>：怎么都喜欢问这个…</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：了解的，要不我先来讲讲ThreadLocal的原理？</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：请开始你的表演吧</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：ThreadLocal是一个壳子，真正的存储结构是ThreadLocal里有ThreadLocalMap这么个内部类</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：而有趣的是，ThreadLocalMap的引用是在Thread上定义的</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：ThreadLocal本身并不存储值，它只是作为key来让线程从ThreadLocalMap获取value</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：所以，得出的结论就是ThreadLocalMap该结构本身就在Thread下定义，而ThreadLocal只是作为key，存储set到ThreadLocalMap的变量当然是线程私有的咯</p>
<p><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtt5mgz1e6j61bw0u0adp02.jpg"></p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：那我想问下，我可以在ThreadLocal下定义Map，key是Thread，value是set进去的值吗？</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：就是说，为啥我要把ThreadLocal做为key，而不是Thread做为key？这样不是更清晰吗？</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：嗯，我明白你的意思。</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：理论上是可以，但没那么优雅。</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：你提出的做法实际上就是所有的线程都访问ThreadLocal的Map，而key是当前线程</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：但这有点小问题，一个线程是可以拥有多个私有变量的嘛，那key如果是当前线程的话，意味着还点做点「手脚」来唯一标识set进去的value</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：假设上一步解决了，还有个问题就是；并发量足够大时，意味着所有的线程都去操作同一个Map，Map体积有可能会膨胀，导致访问性能的下降</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：这个Map维护着所有的线程的私有变量，意味着你不知道什么时候可以「销毁」</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：现在JDK实现的结构就不一样了。</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：线程需要多个私有变量，那有多个ThreadLocal对象足以，对应的Map体积不会太大</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：只要线程销毁了，ThreadLocalMap也会被销毁</p>
<p><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtt5wys45fj61hk0eotba02.jpg"></p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：嗯，了解。</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：<strong>回到ThreadLocal内存泄露上吧，谈谈你对这个的理解呗</strong></p>
<p><span style="color:#000080"><strong>候选者</strong></span>：ThreadLocal内存泄露其实发生的概率非常非常低，我也不知道为什么这么喜欢问。</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：回到原理上，我们知道Thread在创建的时候，会有栈引用指向Thread对象，Thread对象内部维护了ThreadLocalMap引用</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：而ThreadLocalMap的Key是ThreadLocal，value是传入的Object</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：ThreadLocal对象会被对应的栈引用关联，ThreadLocalMap的key也指向着ThreadLocal</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：ThreadLocalRef &amp;&amp; ThreadLocalMap Entry key -&gt;ThreadLocal</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：ThreadRef-&gt;Thread-&gt;ThreadLoalMap-&gt; Entry value-&gt; Object</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：网上大多分析的是ThreadLocalMap的key是弱引用指向ThreadLocal</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：嗯…<strong>要不顺便讲讲Java的4种引用吧</strong></p>
<p><span style="color:#000080"><strong>候选者</strong></span>：强引用是最常见的，只要把一个对象赋给一个引用变量，这个引用变量就是一个强引用</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：强引用：只要对象没有被置null，在GC时就不会被回收</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：软引用相对弱化了一些，需要继承 SoftReference实现</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：软引用：如果内存充足，只有软引用指向的对象不会被回收。如果内存不足了，只有软引用指向的对象就会被回收</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：弱引用又更弱了一些，需要继承WeakReference实现</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：弱引用：只要发生GC，只有弱引用指向的对象就会被回收</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：最后就是虚引用，需要继承PhantomReference实现</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：虚引用的主要作用是：跟踪对象垃圾回收的状态，当回收时通过引用队列做些「通知类」的工作</p>
<p><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtt5yk7shpj61qc07g77502.jpg"></p>
<p><span style="color:#000080"><strong>候选者</strong></span>：了解了这几种引用之后，再回过头来看ThreadLocal</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：嗯..</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：ThreadLocal内存泄露指的是：ThreadLocal被回收了，ThreadLocalMap Entry的key没有了指向</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：但Entry仍然有ThreadRef-&gt;Thread-&gt;ThreadLoalMap-&gt; Entry value-&gt; Object 这条引用一直存在导致内存泄露</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：嗯..</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：为什么我说导致内存泄露的概率非常低呢，我觉得是这样的</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：首先ThreadLocal被两种引用指向</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：1):ThreadLocalRef-&gt;ThreadLocal（强引用）</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：2):ThreadLocalMap Entry key -&gt;ThreadLocal（弱引用）</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：只要ThreadLocal没被回收（使用时强引用不置null），那ThreadLocalMap Entry key的指向就不会在GC时断开被回收，也没有内存泄露一说法</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：通过ThreadLocal了解实现后，又知道ThreadLocalMap是依附在Thread上的，只要Thread销毁，那ThreadLocalMap也会销毁</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：那非线程池环境下，也不会有长期性的内存泄露问题</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：而ThreadLocal实现下还做了些”保护“措施，如果在操作ThreadLocal时，发现key为null，会将其清除掉</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：所以，如果在线程池（线程复用）环境下，如果还会调用ThreadLocal的set/get/remove方法</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：发现key为null会进行清除，不会有长期性的内存泄露问题</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：那存在长期性内存泄露需要满足条件：ThreadLocal被回收&amp;&amp;线程被复用&amp;&amp;线程复用后不再调用ThreadLocal的set/get/remove方法</p>
<p><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtt60g8np7j610g0lwwhi02.jpg"></p>
<p><span style="color:#000080"><strong>候选者</strong></span>：使用ThreadLocal的最佳实践就是：用完了，手动remove掉。就像使用Lock加锁后，要记得解锁</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：那我想问下，<strong>为什么要将ThreadLocalMap的key设置为弱引用呢？强引用不香吗？</strong></p>
<p><span style="color:#000080"><strong>候选者</strong></span>：外界是通过ThreadLocal来对ThreadLocalMap进行操作的，假设外界使用ThreadLocal的对象被置null了，那ThreadLocalMap的强引用指向ThreadLocal也毫无意义啊。</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：弱引用反而可以预防大多数内存泄漏的情况</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：毕竟被回收后，下一次调用set/get/remove时ThreadLocal内部会清除掉</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：<strong>我看网上有很多人说建议把ThreadLocal修饰为static，为什么？</strong></p>
<p><span style="color:#000080"><strong>候选者</strong></span>：ThreadLocal能实现了线程的数据隔离，不在于它自己本身，而在于Thread的ThreadLocalMap</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：所以，ThreadLocal可以只初始化一次，只分配一块存储空间就足以了，没必要作为成员变量多次被初始化。</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：最后想问个问题：<strong>什么叫做内存泄露？</strong></p>
<p><span style="color:#000080"><strong>候选者</strong></span>：…..</p>
<p><span style="color:#000080"><strong>候选者</strong></span>：意思就是：你申请完内存后，你用完了但没有释放掉，你自己没法用，系统又没法回收。</p>
<p><span style="color:#ab4642"><strong>面试官</strong></span>：清楚了</p>
<p><strong>本文总结</strong>：</p>
<ul>
<li><p><strong>什么是ThreadLocal</strong>：它提供了线程的局部变量，每个线程都可以通过set/get来对这个局部变量进行操作，不会和其他线程的局部变量进行冲突，实现了线程的数据隔离。</p>
</li>
<li><p><strong>ThreadLocal实际用处</strong>（举例）：Spring事务，ThreadLocal里存储Map，Key是DataSource，Value是Connection</p>
</li>
<li><p><strong>ThreadLocal设计</strong>：Thread有ThreadLocalMap引用，ThreadLocal作为ThreadLocalMap的Key，set和get进去的Value则是ThreadLocalMap的value</p>
</li>
<li><p><strong>ThreadLocal内存泄露</strong>：ThreadLocal被回收&amp;&amp;线程被复用&amp;&amp;线程复用后不再调用ThreadLocal的set/get/remove方法  才可能发生内存泄露（条件还是相对苛刻）</p>
</li>
<li><p><strong>ThreadLocal最佳实践</strong>：用完就要remove掉</p>
</li>
</ul>
<p><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtm29xysufj60u00u0jte02.jpg"></p>
<p><span align="center">第一时间获取<span style="color:#000080"><strong>BATJTMD一线互联网大厂</strong></span>最新的面试资料以及内推机会关注公众号<span style="color:#ab4642"><strong>「对线<span style="color:#ab4642"><strong>面试官</strong></span>」</strong></span></p>
<img align="center" src='https://tva1.sinaimg.cn/large/008i3skNgy1gtlvty8zo5j60u00u0q5602.jpg' width=300px height=300px />


</div>


    <div class="post-guide">
        <div class="item left">
            
              <a href="/luffy/2021/08/19/02-Java%E5%B9%B6%E5%8F%91/07.%20CountDownLatch%E5%92%8CCyclicBarrier/">
                  <i class="fa fa-angle-left" aria-hidden="true"></i>
                  07. CountDownLatch和CyclicBarrier
              </a>
            
        </div>
        <div class="item right">
            
              <a href="/luffy/2021/08/19/02-Java%E5%B9%B6%E5%8F%91/05.%20%E7%BA%BF%E7%A8%8B%E6%B1%A0/">
                05. 线程池
                <i class="fa fa-angle-right" aria-hidden="true"></i>
              </a>
            
        </div>
    </div>




<script>
	
	
</script>
	</div>
	<button id="totop-toggle" class="toggle"><i class="fa fa-angle-double-up" aria-hidden="true"></i></button>
</body>
</html>
